<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 放置一个弹层 用来编辑新增节点 -->
      <el-dialog :title="formData.id?'编辑权限':'添加权限'" :visible="showDialog" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 靠右的按钮 -->
      <toolBar>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPermission">添加权限</el-button>
        </template>
      </toolBar>
      <!-- 表格 -->
      <el-table border :data="permissionLis" row-key="id" default-expand-all>
        <el-table-column align="center" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template #default="{row}">
            <el-button type="text" :disabled="row.type===2" @click="addPermission(row)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permission'
export default {
  data() {
    return {
      permissionLis: [],
      showDialog: false,
      formData: {
        enVisible: '1',
        name: '',
        code: '',
        description: '',
        type: 1,
        pid: '0'
      },
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  async mounted() {
    // 钩子函数
    // 调用获取所有权限点函数
    this.getPermissionList()
  },
  methods: {
    // 获取所有权限点函数封装
    async getPermissionList() {
      const res = await getPermissionList()
      this.permissionLis = this.handleData(res, '0')
    },
    // 处理组织列表数据函数
    handleData(list, pID) {
      const arr = []
      list.forEach(v => {
        if (v.pid === pID) {
          const temp = this.handleData(list, v.id)
          v.children = temp
          arr.push(v)
        }
      })
      return arr
    },
    // 点击编辑按钮
    async editPermission(id) {
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },
    // 点击添加按钮
    addPermission(data) {
      this.showDialog = true
      if (data.id) {
        this.formData.type = 2
        this.formData.pid = data.id
      }
    },
    // 点击删除按钮
    async delPermission(id) {
      try {
        await this.$confirm('确定删除吗?')
        await delPermission(id)
        this.$message.success('删除成功')
        // 调用获取所有权限点函数
        this.getPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    // 点击弹出层取消按钮
    btnCancel() {
      this.$refs.perForm.resetFields()
      this.formData = {
        enVisible: '1',
        name: '',
        code: '',
        description: '',
        type: 1,
        pid: '0'
      }
      this.showDialog = false
    },
    // 点击弹出层确定按钮
    async btnOK() {
      try {
        await this.$refs.perForm.validate()
        if (this.formData.id) {
          await updatePermission(this.formData)
          this.$message.success('编辑成功')
        } else {
          await addPermission(this.formData)
          this.$message.success('添加成功')
        }
      } catch (error) {
        console.log(error)
      }
      // 调用获取所有权限点函数
      this.getPermissionList()
      this.showDialog = false
    }
  }
}
</script>
<style lang="sass" scoped>

</style>
